package com.yuexun.dhdoctor.module.personalPlan.presenter.impl;

import android.util.Log;

import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.personalPlan.bean.LockTimeBean;
import com.yuexun.dhdoctor.module.personalPlan.bean.OtherPlanBean;
import com.yuexun.dhdoctor.module.personalPlan.model.IPlanMainModel;
import com.yuexun.dhdoctor.module.personalPlan.presenter.IPlanMainPresenter;
import com.yuexun.dhdoctor.module.personalPlan.ui.view.IPlanMainView;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by work-jx on 2016/9/6.
 */
public class PlanMainPresenter implements IPlanMainPresenter {
    private IPlanMainView view;
    private IPlanMainModel model;

    //记录Log
//    private final Logger log = ProUtil.getLogger(LoginPresneter.class);

    public PlanMainPresenter(IPlanMainModel model, IPlanMainView view) {
        this.model = model;
        this.view = view;
    }

    @Override
    public void getMyTask(Map<String, String> praise) {
        model.getMyTask(praise)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(String s) {
                        view.showTask(s);
                    }
                });
    }

    @Override
    public void getOtherTask(Map<String, String> praise) {
        model.getOtherTask(praise)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<OtherPlanBean>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.PullDownRefreshState(AppConfig.DOWNLOAD_NETERROR);
                    }

                    @Override
                    public void onNext(OtherPlanBean otherPlanBean) {
                        if(otherPlanBean.getStatus().equals("200")) {
                            view.PullDownRefreshState(AppConfig.DOWNLOAD_SUCCESS);
                            view.showOtherTask(otherPlanBean.getDatas());
                        }
                        else {
                            view.PullDownRefreshState(AppConfig.DOWNLOAD_NETERROR);
                        }
                    }
                });
    }

    @Override
    public void lockTime(Map<String, String> praise) {
        model.lockTime(praise)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LockTimeBean>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("jx",e.toString());
                    }

                    @Override
                    public void onNext(LockTimeBean lockTimeBean) {
                        view.lockTime(lockTimeBean);
                    }
                });
    }


}
